package web.controller.employer;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.PayService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.WithholdRequest;

/**
 * Created by aceyin on 15/7/13.
 * <p>
 * 拒付工资
 */
@RestController
@RequestMapping(Routes.Employer.WITHHOLD)
public class Withhold extends Base {

    @Autowired
    private PayService payService;

    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse withhold(@RequestBody WithholdRequest request) {
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            return ServiceResponse.fail(result.getMessage());
        }

        long employer = getUserIdFromAuthHeader();
        String reason = request.getReason();
        long recruitment = request.getRecruitment();
        if (request.getUser() != null) {
            return payService.withhold(employer, request.getUser(), recruitment, reason);
        } else {
            return payService.withholdMultiple(employer, recruitment, reason);
        }
    }
}
